package com.cykj.admin.service.impl;

import com.cykj.admin.mapper.CompanyMapper;
import com.cykj.admin.service.ICompanyService;
import com.cykj.admin.vo.CompanyVO;
import com.cykj.common.dto.ResponseDTO;
import com.cykj.common.entity.AdminDO;
import com.cykj.common.entity.CompanyDO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject:@BelonqsPackage:com.cykj.admin.service.impl
 * @Author:jiangna
 * @CreateTime:2025-08-26 15:11
 */
@Service
public class CompanyServiceImpl implements ICompanyService {
    @Autowired
    private CompanyMapper companyMapper;
    @Value("${fileUrl}")
    private String httpPath;
    @Value("${localPath}")
    private String localPath;

    /**
    * @Description: 查询所有的企业信息 / 有条件查询
    * @Param: [companyVO]
    * @return: com.cykj.common.dto.ResponseDTO
    */
    @Override
    public ResponseDTO selectAllCompanyInfo(CompanyVO companyVO) {
        PageInfo<CompanyDO> page;
        //开启分页
        PageHelper.startPage(companyVO.getCurrentPage(), companyVO.getPageSize());
        List<CompanyDO> companyList = companyMapper.selectAllCompanyInfo(companyVO);
        for (CompanyDO item : companyList) {
            item.setLicenseUrl(replaceUrl(item.getLicenseUrl()));
        }
        page = new PageInfo<>(companyList);
        if (page.getTotal() < page.getPageSize() && companyList.size() == 0 && page.getTotal() > 0) {
            companyVO.setCurrentPage(1);
            PageHelper.startPage(companyVO.getCurrentPage(), companyVO.getPageSize());
            List<CompanyDO> companyList2 = companyMapper.selectAllCompanyInfo(companyVO);
            for (CompanyDO item : companyList2) {
                item.setLicenseUrl(replaceUrl(item.getLicenseUrl()));
            }
            page = new PageInfo<>(companyList2);
        }
        return ResponseDTO.success(page);
    }

    private String replaceUrl(String filePath) {
        return filePath.replace("../static", httpPath);
    }
    /**
     * @Description: 路径转换
     * @Param: [fileName]
     * @return: java.lang.String
     */
    private String changeUrl(String fileName) {
        String staticFile = fileName.replace(httpPath, localPath);
        return staticFile.replace("file:", "");
    }

    /**
    * @Description: 修改企业的信息
    * @Param: [companyVO]
    * @return: com.cykj.common.dto.ResponseDTO
    */
    @Override
    public ResponseDTO updateByCompany(CompanyVO companyVO) {
        int result = companyMapper.updateByCompany(companyVO);
        if (result > 0) {
            return ResponseDTO.success(result);
        }return ResponseDTO.error("修改公司信息失败");
    }
}
